package com.ruoyi.develop.goodsReimbursement.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsFee.domain.GoodsFee;
import com.ruoyi.develop.goodsFee.service.IGoodsFeeService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsReimbursement.domain.GoodsReimbursement;
import com.ruoyi.develop.goodsReimbursement.mapper.GoodsReimbursementMapper;
import com.ruoyi.develop.goodsReimbursement.service.IGoodsReimbursementService;
import com.ruoyi.develop.goodsReimbursementDetail.domain.GoodsReimbursementDetail;
import com.ruoyi.develop.goodsReimbursementDetail.service.impl.GoodsReimbursementDetailServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 报销发票信息管理Service业务层处理
 *
 * @author qh
 * @date 2023-01-12
 */
@Service
public class GoodsReimbursementServiceImpl implements IGoodsReimbursementService {
    @Resource
    private GoodsReimbursementMapper goodsReimbursementMapper;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    @Resource
    private GoodsReimbursementDetailServiceImpl goodsReimbursementDetailService;
    @Resource
    private IGoodsFeeService feeService;

    /**
     * 查询报销发票信息管理
     *
     * @param id 报销发票信息管理主键
     * @return 报销发票信息管理
     */
    @Override
    public GoodsReimbursement selectById(String id) {
        GoodsReimbursement entity = goodsReimbursementMapper.selectById(id);
        if (!"2".equals(entity.getType())) {
            GoodsOrderIn orderParams = new GoodsOrderIn();
            orderParams.setImburseId(id);
            List<GoodsOrderIn> orders = goodsOrderInMapper.selectList(orderParams);
            entity.setOrderList(orders);
        } else {
            GoodsFee feeParam = new GoodsFee();
            feeParam.setImburseId(id);
            List<GoodsFee> feeList = feeService.selectList(feeParam);
            entity.setFeeList(feeList);
        }
        return entity;
    }

    /**
     * 查询报销发票信息管理列表
     *
     * @param entity 报销发票信息管理
     * @return 报销发票信息管理
     */
    @Override
    public List<GoodsReimbursement> selectList(GoodsReimbursement entity) {
        return goodsReimbursementMapper.selectList(entity);
    }

    @Override
    public List<GoodsReimbursement> selectListNoScope(GoodsReimbursement entity) {
        return goodsReimbursementMapper.selectList(entity);
    }

    /**
     * 新增报销发票信息管理
     *
     * @param entity 报销发票信息管理
     * @return 结果
     */
    @Override
    public int insert(GoodsReimbursement entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsReimbursementMapper.insert(entity);
    }

    /**
     * 修改报销发票信息管理
     *
     * @param entity 报销发票信息管理
     * @return 结果
     */
    @Override
    public int update(GoodsReimbursement entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsReimbursementMapper.update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public int insertNew(GoodsReimbursement entity) {
        String imbursementId = "";
        if (StringUtils.isBlank(entity.getId())) {
            imbursementId = IdUtils.fastSimpleUUID();
            entity.setId(imbursementId);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        //绑定订单或研发经费中的报销关联信息
        reConnectImburseIdForOrderAndFee(entity);
        return goodsReimbursementMapper.insert(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateNew(GoodsReimbursement entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        String imbursementId = entity.getId();
        //删除订单管理或费用管理中绑定的报销申请信息
        updateImburseIdNULLForOrderAndFee(imbursementId, entity.getType());
        //重新绑定订单或研发经费中的报销关联信息,并计算报销金额
        reConnectImburseIdForOrderAndFee(entity);
        //发放报销资金,将关联入库商品的状态改为"已报销"
        //注释代码 : 20231127已发放逻辑不走此处代码
        /*if (BusinessConstants.STATUS_IMBURSEMENT_FF.equals(entity.getStatus())) {
            GoodsFee feeParam = new GoodsFee();
            feeParam.setImburseId(entity.getId());
            GoodsOrderIn orderInParam = new GoodsOrderIn();
            orderInParam.setImburseId(entity.getId());
            if (entity.getImburseMoney().equals(entity.getRemMoney()) && entity.getRemRemain().equals(BigDecimal.ZERO)) {
                *//*List<GoodsOrderIn> orderListForImburse = entity.getOrderList();
                for (GoodsOrderIn order : orderListForImburse) {
                    goodsOrderInMapper.updateImburseStaus(order);
                }*//*
                feeParam.setStatus("yhx");
                orderInParam.setReimburse("1");
            } else {
                entity.setStatus(BusinessConstants.STATUS_IMBURSEMENT_SHTG);
                feeParam.setStatus("whx");
                orderInParam.setReimburse("0");
            }
            goodsOrderInMapper.updateReimburseStatusByImburseId(orderInParam);
            feeService.updateFeeStatusByImburseId(feeParam);
        }*/

        return goodsReimbursementMapper.update(entity);
    }

    private void reConnectImburseIdForOrderAndFee(GoodsReimbursement entity) {
        String imbursementId = entity.getId();
        if (BusinessConstants.STATUS_IMBURSEMENT_SQZ.equals(entity.getStatus())) {
            BigDecimal imburseMoney = BigDecimal.ZERO;
            if (!"2".equals(entity.getType())) {
                List<GoodsOrderIn> orderListForImburse = entity.getOrderList();
                for (GoodsOrderIn order : orderListForImburse) {
                    order.setImburseId(imbursementId);
                    imburseMoney = imburseMoney.add(order.getCost());
                    goodsOrderInMapper.updateImburseIdForOrder(order);
                }
            } else {
                List<GoodsFee> feeList = entity.getFeeList();
                for (GoodsFee fee : feeList) {
                    fee.setImburseId(imbursementId);
                    imburseMoney = imburseMoney.add(fee.getMoney());
                    feeService.updateImburseIdForFee(fee);
                }
            }
            entity.setImburseMoney(imburseMoney);
            entity.setRemMoney(BigDecimal.ZERO);
            entity.setRemRemain(imburseMoney);
        }
    }

   /* @Override
    @Transactional(readOnly = false)
    public int edit0701(GoodsReimbursement entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        GoodsReimbursementDetail reimbursementDetail = entity.getReimbursementDetail();
        reimbursementDetail.setReimburseId(entity.getId());
        goodsReimbursementDetailService.insert(reimbursementDetail);
        if (entity.getRemRemain() == null) {//剩余发放金额为空,则将申请金额填入
            entity.setRemRemain(entity.getImburseMoney());
        }
        if (entity.getRemMoney() == null) {//已发放金额,则设置为0
            entity.setRemMoney(BigDecimal.ZERO);
        }
        entity.setRemRemain(entity.getRemRemain().subtract(reimbursementDetail.getGrantMoney()));//剩余发放金额
        entity.setRemMoney(entity.getRemMoney().add(reimbursementDetail.getGrantMoney()));//已发放金额
        //发放报销资金,将关联入库商品的状态改为"已报销"
        if (BusinessConstants.STATUS_IMBURSEMENT_FF.equals(entity.getStatus())) {
            if (entity.getImburseMoney().equals(entity.getRemMoney()) && entity.getRemRemain().equals(BigDecimal.ZERO)) {
                List<GoodsOrderIn> orderListForImburse = entity.getOrderList();
                for (GoodsOrderIn order : orderListForImburse) {
                    goodsOrderInMapper.updateImburseStaus(order);
                }
            } else {
                entity.setStatus(BusinessConstants.STATUS_IMBURSEMENT_SHTG);
            }
        }

        return goodsReimbursementMapper.update(entity);
    }*/

    /**
     * 批量删除报销发票信息管理
     *
     * @param ids 需要删除的报销发票信息管理主键
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteByIds(String[] ids) {
        for (String id : ids) {
            /*GoodsOrderIn orderIn = new GoodsOrderIn();
            orderIn.setImburseId(id);
            goodsOrderInMapper.updateByImburseId(orderIn);*/
            updateImburseIdNULLForOrderAndFee(id, "");
        }
        return goodsReimbursementMapper.deleteByIds(ids);
    }

    /**
     * 删除报销发票信息管理信息
     *
     * @param id 报销发票信息管理主键
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteById(String id) {
        updateImburseIdNULLForOrderAndFee(id, "");
        return goodsReimbursementMapper.deleteById(id);
    }

    private void updateImburseIdNULLForOrderAndFee(String imburseId, String type) {
        Map<String, String> orderPara = new HashedMap();
        orderPara.put("imburseId", imburseId);
        if ("0".equals(type) || "1".equals(type)) {
            goodsOrderInMapper.updateImburseIdForOrderByIds(orderPara);
        } else if ("2".equals(type)) {
            feeService.updateImburseIdForFeeByIds(orderPara);
        } else {
            goodsOrderInMapper.updateImburseIdForOrderByIds(orderPara);
            feeService.updateImburseIdForFeeByIds(orderPara);
        }


    }
}
